package com.atguigu.lease.web.admin.controller.apartment;


import com.atguigu.lease.common.result.Result;
import com.atguigu.lease.model.entity.FacilityInfo;
import com.atguigu.lease.model.enums.ItemType;
import com.atguigu.lease.web.admin.service.FacilityInfoService;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;



/*
*  springMVC 中的 controller 可以接受如下几种类型的参数
*  1、请求参数
*  request.getParameter(""), request.getParameterValues("");
*  -@RequestParam()
*
*  2、请求体参数
*  @RequestBody
*
*  3、路径参数
*  @PathVariable
*
*  4、请求头参数
*  @RequestHeader
*
* */


@Tag(name = "配套管理")
@RestController
@RequestMapping("/admin/facility")
public class FacilityController {


    @Autowired
    private FacilityInfoService facilityInfoService;




    @Operation(summary = "[根据类型]查询配套信息列表")
    @GetMapping("list")
    public Result<List<FacilityInfo>> listFacility(@RequestParam(required = false) ItemType type) {

        LambdaQueryWrapper<FacilityInfo> facilityInfoLambdaQueryWrapper = new LambdaQueryWrapper<>();

        // facilityInfoLambdaQueryWrapper.eq(FacilityInfo::getType,type);
        // 如果 type=null 不往后拼接条件，查询所有，如果type！= null 正常查询
        facilityInfoLambdaQueryWrapper.eq(type!=null,FacilityInfo::getType,type);

        List<FacilityInfo> facilityInfoList = facilityInfoService.list(facilityInfoLambdaQueryWrapper);

        return Result.ok(facilityInfoList);

    }

    @Operation(summary = "新增或修改配套信息")
    @PostMapping("saveOrUpdate")
    public Result saveOrUpdate(@RequestBody FacilityInfo facilityInfo) {
        facilityInfoService.saveOrUpdate(facilityInfo);
        return Result.ok();
    }

    @Operation(summary = "根据id删除配套信息")
    @DeleteMapping("deleteById")
    public Result removeFacilityById(@RequestParam Long id) {
        facilityInfoService.removeById(id);
        return Result.ok();
    }

}
